<?php

/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Mazentop\DiscountPrice\Helper;

use Magento\Catalog\Model\Category;
use Magento\Wishlist\Model\Wishlist;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Sales\Model\Order;
use Mazentop\DiscountPrice\Model\ProductCache;

/**
 * Catalog data helper
 * @SuppressWarnings(PHPMD.TooManyFields)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    protected $_category;
    protected $_wishlistCollectionFactory;
    protected $_storeManagerry;
    protected $_stockItemRepository;
    protected $_configurable;
    protected $_ordercollectionFactory;
    protected $_order;
    protected $_checkoutSession;
    protected $_rules;
    protected $_dateHelper;
    protected $_productCache;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context, Category $category,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Helper\Category $categoryHelper,
        CollectionFactory $wishlistCollectionFactory,
        \Magento\CatalogInventory\Api\StockStateInterface $stockItemRepository,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $ordercollectionFactory,
        Order $order,

        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Catalog\Helper\Image $imageHelper,
        \Magento\CatalogRule\Model\Rule $rules,
        \Magento\Framework\Stdlib\DateTime\DateTime $dateHelper,
        ProductCache $productCache
    )
    {
        $this->_productCache = $productCache;
        $this->_category = $category;
        $this->_storeManager = $storeManager;
        $this->_categoryHelper = $categoryHelper;
        $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
        $this->_stockItemRepository = $stockItemRepository;
        $this->_configurable = $configurable;
        $this->_ordercollectionFactory = $ordercollectionFactory;
        $this->_order = $order;
        $this->_checkoutSession = $checkoutSession;
        $this->_imageHelper = $imageHelper;
        $this->_rules = $rules;
        $this->_dateHelper = $dateHelper;
        parent::__construct($context);
    }

    public function DisplayDiscountLabel($_product)
    {
        /* 产品获取折扣比例*/
        $originalPrice = $_product->getPrice();
        $finalPrice = $_product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
        $percentage = 0;
        if ($originalPrice > $finalPrice) {
            $percentage = number_format(($originalPrice - $finalPrice) * 100 / $originalPrice, 0);
        }

        if ($percentage) {
            return $percentage . "%";
        }
    }

    public function getProductDiscountEndDate($_product, $format = 'd F')
    {
        //todo 这里有个性能问题
        //print_r(get_class_methods(get_class($_product)));
        //print_r($_product->getSpecialPrice().'时间');
        /** @var $_product \Magento\Catalog\Model\Product */
        $cacheId = $_product->getId();

        $cacheTag = [];
        $lifeTime = '3600';
   //     var_dump($_product->getId());
/*      foreach ($this->_rules->getResourceCollection() as $rule) {
            if ($rule->getMatchingProductIds()[$_product->getId()][1]) {
                {
                    var_dump($_product->getId().$this->_dateHelper->date($format, $rule->getToDate()));
                    return $end = $this->_dateHelper->date($format, $rule->getToDate());
                }
            }
        }

        return '';*/



        // var_dump($cacheId.$data);



     $data = $this->_productCache->getLowLevelFrontend()->load($_product->getId());
    //var_dump($_product->getId().$data);
       if ($data!='none'&&$data) {

           /* if ($data == 'none') {//这里是么有缓存的意思 擦咯
                $data = '';
            }*/
            return $data;
        } else {
            foreach ($this->_rules->getResourceCollection() as $rule) {
                if ($rule->getMatchingProductIds()[$_product->getId()][1]) {
                        $end = $this->_dateHelper->date($format, $rule->getToDate());
                    //    var_dump('end'.$end);
                         $this->_productCache->getLowLevelFrontend()->save($end, $_product->getId(), $cacheTag, $lifeTime);
                         return  $end;
                }
            }
        }
        return '';
    }

    function getAllcategory()
    {  /*获取所有子分类*/
        $resArr = array();
        $_categories = $this->_categoryHelper->getStoreCategories();

        foreach ($_categories as $key => $subCatid) {
            $resArr[$key]['catename'] = $subCatid->getName();
            $resArr[$key]['url'] = $this->_categoryHelper->getCategoryUrl($subCatid);
            $_subcategory = $this->_category->load($subCatid->getId())->getChildrenCategories();
            if ($_subcategory) {
                foreach ($_subcategory as $k => $_sub) {
                    $resArr[$key]['child'][$k]['subname'] = $_sub->getName();
                    $resArr[$key]['child'][$k]['suburl'] = $_sub->getUrl();
                }
            }
        }

        return $resArr;
    }

    function getWishCount($productId)
    {
        $wishlist = $this->_wishlistCollectionFactory->create()->addFieldToFilter('product_id', $productId);
        if ($wishlist) {
            $count = count($wishlist);
        } else {
            $count = 0;
        }
        return $count;
    }

    public function getStoreCurrency()
    {
        return $this->_storeManager->getStore()->getCurrentCurrency()->getCurrencySymbol();
    }

    public function getStoreCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrency()->getCurrencyCode();
    }

    public function getStockNum($product)
    {  /*获取产品库存*/
        $configurable_products = $this->_configurable->getUsedProducts(
            $product
        );
        $qty = 0;
        if ($configurable_products) {
            foreach ($configurable_products as &$cproduct) {
                $qty += $this->_stockItemRepository->getStockQty($cproduct->getId(), $cproduct->getStore()->getWebsiteId());
            }
        } else {
            $qty = $this->_stockItemRepository->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
        }
        return $qty;
    }

    /* Use this method to get ID */
    public function getRealOrderId()
    {
        $lastorderId = $this->_checkoutSession->getLastOrderId();
        return $lastorderId;
    }

    public function getOrderInfo($orderId)
    {  /*获取订单信息*/
        $lid = $this->getRealOrderId();
        $orderInfo = array();
        $currentorder = $this->_order->load($lid);
        $orderInfo['totalprice'] = $currentorder->getGrandTotal();
        $orderInfo['currency'] = $currentorder->getOrderCurrencyCode();
        $items = $currentorder->getAllItems();
        $a = array();
        foreach ($items as $i) {
            $a[] = $i->getProductId();
        }
        $orderInfo['ProductId'] = implode(',', $a);
        return $orderInfo;
    }

    public function getImageUrl($product)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        if ($product->getProduct()->getId()) {
            $product = $objectManager->create('Magento\Catalog\Model\Product')
                ->load($product->getProduct()->getId());
            $imagewidth = 88;
            $imageheight = 88;
            $image_url = $this->_imageHelper
                ->init($product, 'product_page_image_small')
                ->setImageFile($product->getFile())
                ->resize($imagewidth, $imageheight)
                ->getUrl();
        } else {
            $image_url = $this->_imageHelper
                ->init($product, 'product_page_image_small')
                ->getDefaultPlaceholderUrl('small_image');
        }
        return $image_url;
    }

    public function getCheckoutSession()
    {
        return $this->_checkoutSession;
    }

}
